package com.yaoyong.codegenerator.properties;

import com.yaoyong.codegenerator.constant.DatabaseType;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 姚勇
 */
@ConfigurationProperties(prefix = "code.generator.database")
public class DatabaseProperties {

    /**
     * 数据库类型
     */
    private DatabaseType databaseType = DatabaseType.MYSQL;
    /**
     * 数据库驱动
     */
    private String driverClassName = "com.mysql.cj.jdbc.Driver";
    /**
     * 数据库连接url
     */
    private String url = "jdbc:mysql://localhost:3306/test";
    /**
     * 用户名
     */
    private String username = "root";
    /**
     * 密码
     */
    private String password = "root";

    /**
     * 数据库字段类型和Java类字段类型映射关系
     */
    private static Map<String, String> convertMap = new HashMap<>();

    static {
        convertMap.put("CHAR", "String");
        convertMap.put("VARCHAR", "String");
        convertMap.put("VARCHAR2", "String");
        convertMap.put("NVARCHAR2", "String");
        convertMap.put("TEXT", "String");
        convertMap.put("BLOB", "byte[]");
        convertMap.put("BIGINT", "java.math.BigInteger");
        convertMap.put("NUMBER", "Long");
        convertMap.put("INTEGER", "Long");
        convertMap.put("INT", "Integer");
        convertMap.put("TINYINT", "Integer");
        convertMap.put("SMALLINT", "Integer");
        convertMap.put("MEDIUMINT", "Integer");
        convertMap.put("TIME", "java.time.LocalTime");
        convertMap.put("DATE", "java.time.LocalDate");
        convertMap.put("DATETIME", "java.time.LocalDateTime");
        convertMap.put("TIMESTAMP", "java.time.LocalDateTime");
        convertMap.put("DOUBLE", "Double");
        convertMap.put("FLOAT", "Float");
        convertMap.put("DECIMAL", "java.math.BigDecimal");
        convertMap.put("BIT", "Boolean");
    }

    public DatabaseType getDatabaseType() {
        return databaseType;
    }

    public DatabaseProperties setDatabaseType(DatabaseType databaseType) {
        this.databaseType = databaseType;
        return this;
    }

    public String getDriverClassName() {
        return driverClassName;
    }

    public DatabaseProperties setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
        return this;
    }

    public String getUrl() {
        return url;
    }

    public DatabaseProperties setUrl(String url) {
        this.url = url;
        return this;
    }

    public String getUsername() {
        return username;
    }

    public DatabaseProperties setUsername(String username) {
        this.username = username;
        return this;
    }

    public String getPassword() {
        return password;
    }

    public DatabaseProperties setPassword(String password) {
        this.password = password;
        return this;
    }

    public static Map<String, String> getConvertMap() {
        return convertMap;
    }

    public static void setConvertMap(Map<String, String> convertMap) {
        DatabaseProperties.convertMap = convertMap;
    }
}
